package com.road.vast.springboot;

import com.road.vast.springboot.pojo.Holiday;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author 郭泽鹏
 * @Date 2020/6/23 9:04
 * @Version 1.0
 * 启动流程实例(前提：已经完成流程定义的部署工作)
 * 影响到的表：
 * act_hi_actinst：已完成的活动信息
 * act_hi_identitylink：参与者信息
 * act_hi_procinst：流程实例
 * act_hi_taskinst：任务实例
 * act_ru_execution：执行表
 * act_ru_identitylink：参与者信息
 * act_ru_task：任务
 *
 *
 */
public class ActivitiStartInstance {
    //流程定义使用固定分配方式实例化
//    public static void main(String[] args) {
//        //1、得到 ProcessEngine 对象
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        //2、得到 RuntimeService 对象
//        RuntimeService runtimeService = processEngine.getRuntimeService();
//
//        //3、创建流程实例，参数：流程定义的key
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holiday");
//
//        //4、输出实例相关信息
//        System.out.println("流程部署ID：" + processInstance.getDeploymentId());  //null
//        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());   //holiday:1:4
//        System.out.println("流程实例ID：" + processInstance.getId());    //5001
//        System.out.println("活动ID：" + processInstance.getActivityId());  //null
//
//    }

//    //流程定义使用UEL表达式分配方式实例化
//    public static void main(String[] args) {
//        //1、得到 ProcessEngine 对象
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        //2、得到 RuntimeService 对象
//        RuntimeService runtimeService = processEngine.getRuntimeService();
//
//        //流程定义的key
//        String processDefinitionKey = "holiday2";
//        //设置assignee的取值，
//        Map<String, Object> map = new HashMap<>();
//        map.put("assignee0", "zhangsan");
//        map.put("assignee1", "lisi");
//        map.put("assignee2", "wangwu");
//
//        //3、创建流程实例，参数：流程定义的key
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);
//
//        //4、输出实例相关信息
//        System.out.println("流程部署ID：" + processInstance.getDeploymentId());  //null
//        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());   //holiday:1:4
//        System.out.println("流程实例ID：" + processInstance.getId());    //5001
//        System.out.println("活动ID：" + processInstance.getActivityId());  //null
//
//    }

//    //流程变量的测试:在流程实例化时设置变量的值
//    //查看是否添加流程实例：act_ru_variable act_ge_bytearray
//    public static void main(String[] args) {
//        //1、得到 ProcessEngine 对象
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        //2、得到 RuntimeService 对象
//        RuntimeService runtimeService = processEngine.getRuntimeService();
//
//        //流程定义的key
//        String processDefinitionKey = "holiday3";
//        //设置assignee的取值，
//        Map<String, Object> map = new HashMap<>();
//        Holiday holiday = new Holiday();
//        holiday.setNum(5F);
//        holiday.setAssignee0("aa");
//        holiday.setAssignee1("bb");
//        holiday.setAssignee2("cc");
//        holiday.setAssignee3("dd");
//        map.put("holiday", holiday);
//
//        //3、创建流程实例，参数：流程定义的key
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);
//
//        //4、输出实例相关信息
//        System.out.println("流程部署ID：" + processInstance.getDeploymentId());  //null
//        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());   //holiday:1:4
//        System.out.println("流程实例ID：" + processInstance.getId());    //5001
//        System.out.println("活动ID：" + processInstance.getActivityId());  //null
//
//    }

//    //流程变量的测试:在完成任务时设置变量的值
//    //查看是否添加流程实例：act_ru_variable act_ge_bytearray
//    public static void main(String[] args) {
//        //1、得到 ProcessEngine 对象
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        //2、得到 RuntimeService 对象
//        RuntimeService runtimeService = processEngine.getRuntimeService();
//
//        //流程定义的key
//        String processDefinitionKey = "holiday3";
//        //设置assignee的取值，
//        Map<String, Object> map = new HashMap<>();
//        Holiday holiday = new Holiday();
////        holiday.setNum(5F);
//        holiday.setAssignee0("aa");
//        holiday.setAssignee1("bb");
//        holiday.setAssignee2("cc");
//        holiday.setAssignee3("dd");
//        map.put("holiday", holiday);
//
//        //3、创建流程实例，参数：流程定义的key
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);
//
//        //4、输出实例相关信息
//        System.out.println("流程部署ID：" + processInstance.getDeploymentId());  //null
//        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());   //holiday:1:4
//        System.out.println("流程实例ID：" + processInstance.getId());    //5001
//        System.out.println("活动ID：" + processInstance.getActivityId());  //null
//
//    }

//    //流程变量的测试:通过流程实例设置变量的值
//    //查看是否添加流程实例：act_ru_variable act_ge_bytearray
//    public static void main(String[] args) {
//        //1、得到 ProcessEngine 对象
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        //2、得到 RuntimeService 对象
//        RuntimeService runtimeService = processEngine.getRuntimeService();
//
//        //流程定义的key
//        String processDefinitionKey = "holiday3";
//        //设置assignee的取值，
//        Map<String, Object> map = new HashMap<>();
//        Holiday holiday = new Holiday();
////        holiday.setNum(5F);
//        holiday.setAssignee0("aa");
//        holiday.setAssignee1("bb");
//        holiday.setAssignee2("cc");
//        holiday.setAssignee3("dd");
//        map.put("holiday", holiday);
//
//        //3、创建流程实例，参数：流程定义的key
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);
//
//        //4、输出实例相关信息
//        System.out.println("流程部署ID：" + processInstance.getDeploymentId());  //null
//        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());   //holiday:1:4
//        System.out.println("流程实例ID：" + processInstance.getId());    //5001
//        System.out.println("活动ID：" + processInstance.getActivityId());  //null
//
//        //通过流程实例设置变量的值
//        holiday.setNum(1F);
//        map = new HashMap<>();
//        map.put("holiday", holiday);
//
//        runtimeService.setVariables(processInstance.getId(), map);
//    }

//    //组任务测试
//    //查看是否添加流程实例：act_ru_variable act_ge_bytearray
//    public static void main(String[] args) {
//        //1、得到 ProcessEngine 对象
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        //2、得到 RuntimeService 对象
//        RuntimeService runtimeService = processEngine.getRuntimeService();
//
//        //流程定义的key
//        String processDefinitionKey = "holiday4";
//
//        //3、创建流程实例，参数：流程定义的key
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);
//
//        //4、输出实例相关信息
//        System.out.println("流程部署ID：" + processInstance.getDeploymentId());  //null
//        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());   //holiday:1:4
//        System.out.println("流程实例ID：" + processInstance.getId());    //5001
//        System.out.println("活动ID：" + processInstance.getActivityId());  //null
//    }

//    //排他网关测试
//    //查看是否添加流程实例：act_ru_variable act_ge_bytearray
//    public static void main(String[] args) {
//        //1、得到 ProcessEngine 对象
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        //2、得到 RuntimeService 对象
//        RuntimeService runtimeService = processEngine.getRuntimeService();
//
//        //流程定义的key
//        String processDefinitionKey = "holiday6";
//        Map<String, Object> map = new HashMap<>();
//        Holiday holiday = new Holiday();
//        holiday.setNum(3F);
//        holiday.setAssignee0("zhangsan");
//        holiday.setAssignee1("lisi");
//        holiday.setAssignee2("wangwu");
//        holiday.setAssignee3("zhaoliu");
//        map.put("holiday", holiday);
//
//        //3、创建流程实例，参数：流程定义的key
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);
//
//        //4、输出实例相关信息
//        System.out.println("流程部署ID：" + processInstance.getDeploymentId());  //null
//        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());   //holiday:1:4
//        System.out.println("流程实例ID：" + processInstance.getId());    //5001
//        System.out.println("活动ID：" + processInstance.getActivityId());  //null
//    }

    //排他网关测试
    //查看是否添加流程实例：act_ru_variable act_ge_bytearray
    public static void main(String[] args) {
        //1、得到 ProcessEngine 对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2、得到 RuntimeService 对象
        RuntimeService runtimeService = processEngine.getRuntimeService();

        //流程定义的key
        String processDefinitionKey = "examine";

        //3、创建流程实例，参数：流程定义的key
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);

        //4、输出实例相关信息
        System.out.println("流程部署ID：" + processInstance.getDeploymentId());  //null
        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());   //holiday:1:4
        System.out.println("流程实例ID：" + processInstance.getId());    //5001
        System.out.println("活动ID：" + processInstance.getActivityId());  //null
    }
}
